package com.ibm.epss.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (isWifiAvailable(context)) {
            Log.d("TONY", "Connected!!!(recever)");
            try {
                while (MQTTHelper.Connect() != 0) {
                    Thread.sleep(3000);
                }
                MQTTHelper.Subscribe();
            } catch (InterruptedException e) {
            }
        }

    }

    public boolean isWifiAvailable(Context context) {
        boolean rc = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //cm.getActiveNetworkInfo().isConnectedOrConnecting();
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        rc = wifi.isConnected() || mobile.isConnected();
        return rc;
    }
}
